import { TitleMessageEnum } from '@/enums/titleMessageEnum';
import useEventListener from '@/hooks/event/useEventListener';

const useTitleMessage = () => {
  const onVisibilitychangeEventHandler = () => {
    const visibilityState = document.visibilityState;
    document.title = visibilityState === 'hidden' ? TitleMessageEnum.LEFT : TitleMessageEnum.WELCOME;
  };

  const { removeEvent } = useEventListener({ eventName: 'visibilitychange', listener: onVisibilitychangeEventHandler });

  onMounted(() => {
    // 初始化执行一次保证标题正确
    onVisibilitychangeEventHandler();
  });

  onBeforeUnmount(() => removeEvent && removeEvent());
};

export default useTitleMessage;
